package com.sg.service.biz.framework.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.framework.req.QueryInductionDeptListComReqDto;
import com.sg.dto.biz.framework.req.QueryMemberInductionDeptListComReqDto;
import com.sg.dto.biz.framework.req.QueryReceiveFieldDetailReqDto;
import com.sg.dto.biz.framework.res.DeptDto;
import com.sg.dto.biz.framework.res.QueryInductionDeptListComRespDto;
import com.sg.dto.biz.framework.res.QueryMemberInductionDeptListComRespDto;
import com.sg.dto.biz.framework.res.QueryReceiveFieldDetailRespDto;
import com.sg.service.biz.framework.InductionRecordService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class TargetCalcServiceImpl
        implements com.wicket.okrframework.biz.service.TargetCalcService {

    @Resource
    private RedisUtil redisUtil;
    //@Resource
    //private MCustomFields2Service mCustomFields2Service;
    @Resource
    private NbTargetCalc nbTargetCalc;
    @Resource
    private InductionRecordService inductionRecordService;

    /**
     * D1查询身份人员就职的部门列表(公共)[9369]
     * gen by moon at 4/10/2024, 11:11:05 PM
     */
    @Trace(operationName = "D1查询身份人员就职的部门列表(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryMemberInductionDeptListComRespDto queryMemberInductionDeptListCom(QueryMemberInductionDeptListComReqDto reqDto) {


        QueryReceiveFieldDetailRespDto receptionServiceRes_1 = null;
//步骤0: D1查询就职的部门列表(公共) - queryInductionDeptListCom
        QueryInductionDeptListComRespDto queryInductionDeptListComRespDto = null;
        QueryInductionDeptListComReqDto queryInductionDeptListComReqDto = new QueryInductionDeptListComReqDto();
        if (reqDto != null) {
            queryInductionDeptListComReqDto.setInductionUnitTypeCode(reqDto.getInductionUnitTypeCode());//SimpleFieldAssign//sourceId:1718277_1
            queryInductionDeptListComReqDto.setOriginalRoleMemberId(reqDto.getOriginalRoleMemberId());//SimpleFieldAssign//sourceId:1718278_1
            queryInductionDeptListComReqDto.setIsTransaction(reqDto.getIsTransaction());//SimpleFieldAssign//sourceId:1718279_1
            queryInductionDeptListComReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:1718280_1
            queryInductionDeptListComReqDto.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:1718281_1
        }

        /*D1查询就职的部门列表(公共)[9362]   */

        queryInductionDeptListComRespDto = inductionRecordService.queryInductionDeptListCom(queryInductionDeptListComReqDto)/*vcase invoke isSameApp*/;


//步骤1: M1-获取接收字段（特殊方法） - queryReceiveFieldDetail
        //ModelCode: receptionService
        QueryReceiveFieldDetailRespDto receptionServiceRes = null;
        if (queryInductionDeptListComRespDto != null) {
            QueryReceiveFieldDetailReqDto receptionServiceReq = new QueryReceiveFieldDetailReqDto();
            if (queryInductionDeptListComRespDto != null && queryInductionDeptListComRespDto.getOrgInductionRecordList() != null && !CollectionUtil.isEmpty(queryInductionDeptListComRespDto.getOrgInductionRecordList())) {
                receptionServiceReq.setDeptList(//objList-to-objLists
                        queryInductionDeptListComRespDto.getOrgInductionRecordList().stream().map(item -> {
                            DeptDto elm = new DeptDto();
                            if (item != null) {
                                elm.setDeptId(item.getEntityId());//SimpleFieldAssign//sourceId:342761_2
                            }
                            return elm;
                        }).collect(Collectors.toList()));//objList-to-objLists//sourceId:1718559_1
            }

            /*M1接收就职的部门出参[3715]  用于特殊方法接收上游入参。 */

            receptionServiceRes = nbTargetCalc.queryReceiveFieldDetail(receptionServiceReq);


            receptionServiceRes_1 = receptionServiceRes;
        }

        QueryMemberInductionDeptListComRespDto retData = new QueryMemberInductionDeptListComRespDto();
        if (receptionServiceRes_1 != null && receptionServiceRes_1.getDeptList() != null && !CollectionUtil.isEmpty(receptionServiceRes_1.getDeptList())) {
            retData.setDeptList(receptionServiceRes_1.getDeptList().stream().map(item -> item.getDeptId())
                    .collect(Collectors.toList()));/*list-to-strings*///sourceId:1718662_1
        }


        return retData;
    }
    //
}
